﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Threading;
using Bookvar.Common.ErrorSubmitterService;
using Bookvar.ServiceModel;

namespace Bookvar.Common
{
	public class ErrorReporter : IErrorReporter
	{
		#region IErrorReporter Members

		[SuppressMessage( "Microsoft.Performance", "CA1822:MarkMembersAsStatic" )]
		public bool ReportError( ErrorReport errorReport )
		{
			try
			{
				using ( ErrorSubmitterServiceSoapClient proxy = new ErrorSubmitterServiceSoapClient() )
				{
					proxy.ReportError( errorReport );
				}
			}
			catch ( WebException )
			{
				return false;
			}
			catch ( HttpListenerException )
			{
				return false;
			}

			return true;
		}

		public bool ReportError( string message )
		{
			var errorReport = new ErrorReport();
			errorReport.ErrorMessage = message;
			errorReport.ErrorTypeID = 1;
			errorReport.ReportBy = Thread.CurrentPrincipal.Identity.Name;
			errorReport.ReportOn = DateTime.Now;

			return ReportError( errorReport );
		}

		public bool ReportError( object value )
		{
			var errorReport = value as ErrorReport;
			if ( errorReport == null )
			{
				return ReportError( value.ToString() );
			}

			return ReportError( errorReport );
		}

		#endregion
	}
}